﻿using Google.Protobuf.WellKnownTypes;
using guidHelper;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Newtonsoft.Json;
using QYWXUpdate.DataModel;
using System.Net.Http.Headers;
using System.Text;

namespace MyDapperApi.Services.BaiLianAI;

public class TestBaiLianService : ITestBaiLianService
{
    private readonly IConfiguration _configuration;
    private readonly IDateBaseService _dateBaseService;
    private readonly IHttpClientFactory _httpClientFactory;
    private readonly IBaiLianAIBaseService _biLianAIBaseService;

    public TestBaiLianService(IConfiguration configuration,
        IDateBaseService dateBaseService,
        IHttpClientFactory httpClientFactory,
        IBaiLianAIBaseService biLianAIBaseService)
    {
        _configuration = configuration;
        _dateBaseService = dateBaseService;
        _httpClientFactory = httpClientFactory;
        _biLianAIBaseService = biLianAIBaseService;
    }


    /// <summary>
    /// 嵌套在请求体中的 input 结构
    /// </summary>
    public class AppInput
    {
        [JsonProperty("prompt")]
        public string Prompt { get; set; } = string.Empty;

        // 如果您的应用需要 session_id 或其他自定义参数，它们也可能嵌套在这里
        // [JsonProperty("session_id")]
        // public string SessionId { get; set; }
    }


    /// <summary>
    /// 完整的百炼应用 API 请求体结构（针对工作流/高级智能体）
    /// </summary>
    public class BailianAppRequest
    {
        [JsonProperty("input")]
        public AppInput Input { get; set; } = new AppInput();

        // 默认可以是一个空的 JSON 对象 {}
        [JsonProperty("parameters")]
        public object Parameters { get; set; } = new object();

        // 默认可以是一个空的 JSON 对象 {}
        [JsonProperty("debug")]
        public object Debug { get; set; } = new object();
    }

    /// <summary>
    /// biz_params 结构，用于传递自定义参数
    /// </summary>
    public class BizParams
    {
        [JsonProperty("user_prompt_params")]
        public Dictionary<string, object> UserPromptParams { get; set; } = new Dictionary<string, object>();

        // 如果您的应用使用了自定义插件，可能需要传递 user_defined_params
        // [JsonProperty("user_defined_params")]
        // public Dictionary<string, object> UserDefinedParams { get; set; } = new Dictionary<string, object>();
    }

    public async Task<CommonResponse> TestPost()
    {
        var result = new CommonResponse();

        var AppId = "f547bf9ea3ea40238617b81ad7669632";
        var ApiUrl = $"https://dashscope.aliyuncs.com/api/v1/apps/{AppId}/completion";
        var ApiKey = "sk-2b651d6f04da462dbc5931db51ff6f3f";

        // 1. 构造请求内容
        var appRequest = new BailianAppRequest();

        var promot = new AppInput()
        {
            Prompt = "帮我分析一下oa流程的操作方式"
        };

        appRequest.Input = promot;

        // 2. 序列化为 StringContent
        var contentToSend = new StringContent(
            JsonConvert.SerializeObject(appRequest),
            Encoding.UTF8,
            "application/json");

        try
        {
            // 3. 调用 API (使用您之前写的 BailianApiPost 方法)
            var response = await _biLianAIBaseService.BailianApiPost(contentToSend, ApiUrl, ApiKey);

            // 4. 解析结果 (由于是应用调用，结果通常在 output/text 或 output/choices 中)
            // 如果您使用的是兼容模式的响应结构：
            var answer = response?.Output.Text;

            // 如果返回的是纯文本，则在 output.text 字段：
            // var answer = response?.Output?.Text; 

            result.SetSuccess(answer);
        }
        catch (Exception ex)
        {
            result.SetError();
            return result;
        }

        return result;
    }
}